import 'package:flutter/material.dart';

///路由跳转统一采用FZRouteName
class RouteName {
  final String pageName;

  const RouteName(this.pageName);
}

class NavigatorUtil {
  static Future<T?> pushRouteName<T extends Object?>(
    BuildContext context,
    RouteName routeName, {
    Object? arguments,
  }) {
    return Navigator.of(
      context,
    ).pushNamed(routeName.pageName, arguments: arguments);
  }

  static Future<T?>
  pushReplacementWithRouteName<T extends Object?, TO extends Object?>(
    BuildContext context,
    RouteName routeName, {
    TO? result,
    Object? arguments,
  }) {
    return Navigator.of(context).pushReplacementNamed(
      routeName.pageName,
      arguments: arguments,
      result: result,
    );
  }

  static Future<T?> popAndPushRouteName<T extends Object?, TO extends Object?>(
    BuildContext context,
    RouteName routeName, {
    TO? result,
    Object? arguments,
  }) {
    return Navigator.of(
      context,
    ).popAndPushNamed(routeName.pageName, arguments: arguments, result: result);
  }

  static Future<T?> pushRouteNamedAndRemoveUntil<T extends Object?>(
    BuildContext context,
    RouteName newRouteName,
    RoutePredicate predicate, {
    Object? arguments,
  }) {
    return Navigator.of(context).pushNamedAndRemoveUntil(
      newRouteName.pageName,
      predicate,
      arguments: arguments,
    );
  }

  ///清除以前路由页面
  static void popUntil(BuildContext context) {
    Navigator.popUntil(context, (route) {
      return route.navigator?.canPop() ?? false;
    });
  }

  ///直接使用原生的Navigator push出页面
  static Future<T?> pushRoute<T extends Object>(
    BuildContext context,
    Widget page,
  ) {
    return Navigator.of(context).push(
      MaterialPageRoute<T>(
        builder: (context) {
          return page;
        },
      ),
    );
  }

  ///直接使用原生的Navigator 从下到上弹出页面
  static Future<T?> popUpRoute<T extends Object>(
    BuildContext context,
    Widget page,
  ) {
    return Navigator.of(context).push(
      MaterialPageRoute<T>(
        fullscreenDialog: true,
        builder: (context) {
          return page;
        },
      ),
    );
  }

  ///直接使用原生的Navigator pushReplacement页面
  static Future<T?> pushReplacement<T extends Object>(
    BuildContext context,
    Widget page,
  ) {
    return Navigator.of(context).pushReplacement(
      MaterialPageRoute<T>(
        builder: (context) {
          return page;
        },
      ),
    );
  }
}
